package ast;

import java.util.ArrayList;

import symbol.astPreprocessContext;

public class ClassDef extends AstItem {
	// 类的定义
	public String className;
	// DataSignature类
	public ArrayList<AstItem> classAttrs = new ArrayList<AstItem>();
	// Statement类
	public ArrayList<AstItem> attrAssign = new ArrayList<AstItem>();
	// FuncDef类
	public ArrayList<AstItem> classConstructors = new ArrayList<AstItem>();
	// FuncDef类
	public ArrayList<AstItem> classMethods = new ArrayList<AstItem>();
	
	private String getStructDef() {
		// 结构体定义
		String result = "struct " + className + " {\n";
		for (AstItem attr : classAttrs) {
			assert (attr instanceof PrimaryDataSignature) || (attr instanceof ComplexDataSignature);
			if (attr instanceof PrimaryDataSignature) {
				// 非数组
//				System.out.println(((PrimaryDataSignature) attr).getDeclString());
				result += "\t" + ((PrimaryDataSignature) attr).getDeclString() + " " + attr.toString();
				if (((PrimaryDataSignature) attr).dataType.equals("string")) {
					result += "[100]";
				}
				result += ";\n";
			} else {
				// 数组
				result += "\t" + ((ComplexDataSignature) attr).toString() + ";\n";
			}
//			if (attr instanceof PrimaryDataSignature) {
//				result += "\t" + ((PrimaryDataSignature) attr).toString() + ";\n";
//			} else {
//				// 数组定义
//				result += "\t" + ((ComplexDataSignature) attr).toString() + ";\n";
//			}
		}
		result += "};\n\n";
		return result;
	}

	public String toString() {
		String result = getStructDef();
		for (AstItem x : classConstructors) {
			result += x.toString();
		}
		if (classConstructors.size() == 0) {
			// 如果没有构造器
			result += "struct " + className +"* _" + className + "_constructor_()  {\n"
					+ "struct " + className + "* this = (struct " + className + "*) malloc(sizeof(struct " + className + "));\n"
					+ "return this;\n"
					+ "}\n";
			// 把无参构造器加入符号表
			astPreprocessContext.getContext().funcTypeMap.put("_{}_constructor_".format(className), "func<" + className + ">");
		}
		for (AstItem x : classMethods) {
			result += x.toString();
		}
		return result;
	}
	
	public void preprocess(AstItem father) {
		astPreprocessContext.getContext();
//		System.out.println(className);
		// 全局记录这个类的初始赋值信息
		astPreprocessContext.addClassAttrAssign(className, attrAssign);
		astPreprocessContext.updateNowClass(className);
		astPreprocessContext.addClass(className);
		for (AstItem x : classAttrs) {
			assert (x instanceof PrimaryDataSignature) || (x instanceof ComplexDataSignature);
			if (x instanceof PrimaryDataSignature) {
				astPreprocessContext.class_addSymbolInfo(((PrimaryDataSignature) x), className);
			} else {
				astPreprocessContext.class_addSymbolInfo_array(((ComplexDataSignature) x), className);
			}
			x.preprocess(this);
		}
		for (AstItem x : attrAssign) {
			x.preprocess(this);
		}
		for (AstItem x : classConstructors) {
			// 类的构造器
			x.preprocess(this);
//			System.out.println(astPreprocessContext.toDebugString());
		}
		for (AstItem x : classMethods) {
			// 类的方法
			x.preprocess(this);
		}
		astPreprocessContext.removeNowClass();
	}
	
	@Override
	public String toJsonString() {
		String json = "{";
		json += "\"Type\": \"ClassDef\",";
		json += "\"ClassName\": \"";
		json += className;
		json += "\",";
		json += "\"ClassAttrs\": [";
		for (AstItem astItem: classAttrs) {
			json += astItem.toJsonString();
			json += ",";
		}
		json += "],";
		json += "\"AttrAssigns\": [";
		for (AstItem astItem: attrAssign) {
			json += astItem.toJsonString();
			json += ",";
		}
		json += "],";
		json += "\"ClassConstructors\": [";
		for (AstItem astItem: classConstructors) {
			json += astItem.toJsonString();
			json += ",";
		}
		json += "],";
		json += "\"ClassMethods\": [";
		for (AstItem astItem: classMethods) {
			json += astItem.toJsonString();
			json += ",";
		}
		json += "]}";
		return json;
	}
}
